using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PrintScreen;

namespace PrintScreenFull
{
    
    public partial class AddNewAction : Form
    {
        ActionToDo first;
        ImageInfo m_i;
        public enum eAction
        {
                Email,
                OpenFolder,
                OpenImage,
                RunProgram

        }
        public AddNewAction(ImageInfo i)
        {
            InitializeComponent();
            m_i = i;
        }

        private void AddNewAction_Load(object sender, EventArgs e)
        {
            first = new ActionToDo_None(m_i);
            int height =tblActions.Height;
            AddActions(cmbActions);
            while (tblActions.RowStyles.Count > 1)
                tblActions.RowStyles.RemoveAt(tblActions.RowStyles.Count - 1);

            tblActions.RowStyles[0].SizeType = SizeType.AutoSize;
            ActionToDo a = first;
            for (int i = 2; i < 10; i++)
            {
                tblActions.RowCount++;
                a.NextAction = new ActionToDo_None(m_i);
                a = a.NextAction;

                Label l =new Label();
                l.Text = "Action " + i;
                this.tblActions.Controls.Add(l,0, i-1);
                l.Dock = DockStyle.Fill;
                ComboBox cmb = new ComboBox();
                
                cmb.Tag = i;
                AddActions (cmb);
                this.tblActions.Controls.Add(cmb, 1, i-1);
                cmb.Dock = DockStyle.Fill;
                tblActions.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                
            }
        }

        private void AddActions(ComboBox cmb)
        {
            
            cmb.SelectedIndexChanged += new EventHandler(cmbActions_SelectedIndexChanged);
            cmb.DisplayMember = "Value";
            cmb.ValueMember = "Key";
            foreach (int i in Enum.GetValues(typeof(eAction)))
            {
                cmb.Items.Add(new KeyValuePair<int, string>(i, ((eAction)i).ToString()));
            }
            cmb.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void cmbActions_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = sender as ComboBox;
            int i = int.Parse(cmb.Tag.ToString());
            ActionToDo current = first;
            while (i > 1)
            {
                
                current = first.NextAction;
                i--;
            }
            //save next action
            ActionToDo save = current.NextAction;
            KeyValuePair<int, string> sel =(KeyValuePair<int, string>) cmb.SelectedItem;
            current = ActionToDoSelector(sel.Key);
            current.NextAction = save;
            propAct.SelectedObject = current;
            

        }

        private ActionToDo ActionToDoSelector(int val)
        {
            eAction e = (eAction)val;
            switch (e)
            {
                case eAction.Email:
                    return new ActionToDo_Email(m_i);
                case eAction.OpenFolder:
                    return new ActionToDo_RunDir(m_i);
                case eAction.OpenImage:
                    return new ActionToDo_RunFiles(m_i);
                case eAction.RunProgram:
                    return new ActionToDo_Run(m_i);
                default://TODO  throw error
                    return null;
            }
            
        }
    }
}